package com.apestech.api.xml.esb;

import com.apestech.formwork.xml.XmlQuery;
import com.apestech.framework.json.JSONObject;
import com.apestech.framework.xml.plugin.XmlIdVar;
import com.apestech.framework.xml.pub.core.ElementUtil;
import com.apestech.framework.xml.pub.core.NodeJson;
import com.apestech.framework.xml.pub.core.XmlFile;
import org.dom4j.Element;

import java.util.List;

/**
 * @author hup
 * @create 2018-12-11-17:04
 */
public class XmlPasteEsb extends ElementUtil {
    private Element root;
    private Element node;
    private JSONObject result=new JSONObject();
    private JSONObject resultSingle=new JSONObject();
    private XmlQuery xmlQuery;

    public XmlPasteEsb() throws Exception {
        xmlQuery=new XmlQuery();
    }

    public XmlPasteEsb(String xmlString) throws Exception {
        xmlQuery=new XmlQuery();
        xmlQuery.setRoot(getElementFromString(xmlString));
        root=xmlQuery.getRoot();
    }

    public XmlPasteEsb(String xmlString, String id, String type) throws Exception {
        xmlQuery=new XmlQuery();
        xmlQuery.setRoot(getElementFromString(xmlString));
        node=xmlQuery.getNode(id, type);
        root=xmlQuery.getRoot();
    }

    public XmlPasteEsb(XmlQuery xmlQuery, Element node){
        this.xmlQuery=xmlQuery;
        this.node=node;
        root=xmlQuery.getRoot();
    }

    public XmlPasteEsb(String id, String type, XmlQuery xmlQuery) throws Exception {
        this.xmlQuery=xmlQuery;
        this.xmlQuery.setNodename(type);
        node=this.xmlQuery.getNode(id);
        root=this.xmlQuery.getRoot();
    }

    public XmlPasteEsb(String id, String str, XmlIdVar.STR_TYPE str_type) throws Exception {
        this.xmlQuery=new XmlQuery(str, str_type);
        node=this.xmlQuery.getNode(null,id);
        root=this.xmlQuery.getRoot();
    }

    public void setNode(String xpath) throws Exception {
        if(this.root.selectSingleNode(xpath)==null) throw new Exception("传入结点ID ["+xpath+"]有误!");
        this.node= (Element) this.root.selectSingleNode(xpath);
        result=new JSONObject();
    }

    private Element getElementFromString(String xmlString) throws Exception {
        if(xmlString==null){
            throw new Exception("请检查下代码：传入参数为XML文件名或对应String！");
        }
        XmlFile file=new XmlFile();
        if(xmlString.indexOf("<")>=0) file.loadFromString(xmlString, false);
        else file.loadFromFile(xmlString);
        Element root=file.getRoot();
        return root;
    }

    public void getNodeNameJson(String nodename) throws Exception {
        xmlQuery.setNodename(nodename);
        node=xmlQuery.getNode(null,nodename);
        expain();
    }

    public void expain(Element root, Element node) throws Exception {
        this.root=root;
        this.node=node;
        xmlQuery.setRNode(root, node);
        expain();
    }

    public void expain() throws Exception {
         if(node==null) return;
        if(!getNode(node)){
            List<?> list=node.content();
            int len=list.size();
            for(int i=0; i<len; i++){
                String scalss=list.get(i).getClass().getName();
                if(scalss.equals("org.dom4j.tree.DefaultElement")){
                    getNode((Element) list.get(i));
                }
            }
        }
    }

    private boolean getNode(Element eml) throws Exception {
        boolean flag=false;
        String name=eml.getName();
        switch(name){
            case "fieldChange":{
                resultSingle=new JSONObject();
                List<?> list=eml.content();
                int len=list.size();
                for(int i=0; i<len; i++){
                    String scalss=list.get(i).getClass().getName();
                    if(scalss.equals("org.dom4j.tree.DefaultElement")){
                        Element ele=(Element) list.get(i);
                        resultSingle.put(ele.getName(), ele.getTextTrim());
                    }
                }
                flag=true;
                break;
            }
            default:{
                NodeJson json=new NodeJson(eml);
                json.expain();
                result.putAll(json.getResult());
                resultSingle=json.getResult();
                flag=true;
            }
        }
        return flag;
    }

    public JSONObject getResult() {
        return result;
    }

    public JSONObject getResultSingle() {
        return resultSingle;
    }

    public Element getRoot() {
        return root;
    }

    public void setNode(Element node) {
        this.node = node;
    }

    public XmlQuery getXmlQuery() {
        return xmlQuery;
    }
}
